package com.superhelper.app.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.superhelper.app.controller.base.AbstractController;
import com.superhelper.app.domain.AuserShopAudio;
import com.superhelper.app.domain.Resp;
import com.superhelper.app.domain.RespShop;
import com.superhelper.app.service.DeliverService;
import com.superhelper.app.service.MtService;
import com.superhelper.app.service.ShopService;
import com.superhelper.common.Consts;
import com.superhelper.common.domain.DeliverAccount;
import com.superhelper.common.utils.JsonHelper;

@Controller
@RequestMapping("v1/shop")
public class ShopController extends AbstractController {

	@Autowired
	private ShopService shopService;
	@Autowired
	private MtService mtService;
	@Autowired
	private DeliverService deliverService;

	/**
	 * 店铺状态列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("status")
	@ResponseBody
	public Resp status(@RequestParam Long userId) throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		List<RespShop> shops = shopService.getValidAuserShopsByAuserId(userId);
		List<DeliverAccount> delivers = deliverService
				.getDeliverAccountList(userId);
		if (delivers.size() > 0) {
			int waitTotal = deliverService.getWaitingTask(userId);
			for (DeliverAccount d : delivers) {
				d.setDesc("余额充足");
				d.setWaitTotal(waitTotal);
			}
			result.put("delivers", delivers);
		}
		result.put("shops", shops);
		return success(result);
	}

	/**
	 * 跳转授权页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("auth")
	@ResponseBody
	public Resp auth(@RequestParam Long userId, @RequestParam String type)
			throws IOException {
		String authUrl = shopService.getAuthUrl(userId.intValue(), type);
		return success(authUrl);
	}

	/**
	 * 接单设置
	 * 
	 * @return params ["orderAuto":0,"deliverType":1,"shopId":18,"isOpen"
	 *         :true,"shop_type":"mt"}]
	 * @throws Exception
	 * @throws IOException
	 */
	@RequestMapping("order/conf")
	@ResponseBody
	public Resp orderConf(@RequestParam Long userId, @RequestParam String params)
			throws Exception {

		if (StringUtils.isBlank(params)) {
			return fail("保存失败");
		}
		List<RespShop> shops = JsonHelper.fromJson(params,
				new TypeReference<List<RespShop>>() {
				});
		for (RespShop s : shops) {
			s.setAuserId(userId.intValue());
		}
		int res = shopService.updateConf(shops, userId);
		return res > 0 ? success("保存成功") : fail("保存失败");
	}

	/**
	 * bd门店绑定
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("bdBound")
	@ResponseBody
	public Resp bdBound(@RequestParam Long userId, @RequestParam String source,
			@RequestParam String shopId) throws IOException {
		int res = shopService.bdBound(userId.intValue(), source, shopId);
		if (res == -1) {
			return fail("店铺ID错误");
		} else {
			return res > 0 ? success("绑定成功") : fail("绑定失败");
		}
	}

	// /**
	// * 设置店铺状态
	// *
	// * @return
	// * @throws Exception
	// * @throws IOException
	// */
	// @RequestMapping("open")
	// @ResponseBody
	// public Resp open(@RequestParam Long shopId, @RequestParam String type,
	// @RequestParam Integer state, HttpServletResponse response)
	// throws Exception {
	// boolean f = shopService.openShop(shopId, state);
	// return f ? success() : fail("操作失败");
	// }
}
